package spring.in.action.chapter_04;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 观众
 * 
 * @author MRZC
 *
 */
@Aspect
@Component
public class Audience
{

    @Pointcut("execution(* spring.in.action.chapter_04.IPerformance.perform1(..))")
    public void performance1()
    {
    }

    @Pointcut("execution(* spring.in.action.chapter_04.IPerformance.perform2(..))")
    public void performance2()
    {
    }

    @Before("performance1()")
    public void silenceCellPhone()
    {
        System.out.println("关闭手机");
    }

    @Before("performance1()")
    public void takeSeats()
    {
        System.out.println("坐下");
    }

    @AfterReturning("performance1()")
    public void applause()
    {
        System.out.println("鼓掌");
    }

    @AfterThrowing("performance1()")
    public void demandRefund()
    {
        System.out.println("要求退款");
    }

    @Around("performance2()")
    public void watchPerformance(ProceedingJoinPoint jp)
    {
        try
        {
            silenceCellPhone();
            takeSeats();
            jp.proceed();
            applause();
        } catch (Throwable e)
        {
            demandRefund();
            e.printStackTrace();
        }
    }
}
